
/**
 * @Project : Clion_ProgrammingExercises
 * @File : 0837
 * @Author : WangRuoyu 
 * @Date : 2023/5/21 15:41
 */

#include "iostream"

using namespace std;

const int N = 1e5 + 10;
int f[N];
int num[N];

int find(int x) {
    if (f[x] != x) f[x] = find(f[x]);
    return f[x];
}

int main() {
    int n, k;
    scanf("%d %d", &n, &k);
    for (int i = 0; i <= n; ++i) {
        f[i] = i;
        num[i] = 1;
    }

    for (int i = 0; i < k; ++i) {
        string s;
        int a, b;
        cin >> s >> a;
        if (s == "C") {
            cin >> b;
            if (find(a) != find(b)) {
                num[find(b)] += num[find(a)];
                f[find(a)] = find(b);
            }
        } else if (s == "Q1") {
            cin >> b;
            if (find(a) == find(b)) {
                puts("Yes");
            } else {
                puts("No");
            }
        } else if (s == "Q2") {
            printf("%d\n", num[find(a)]);
        }
    }
    return 0;
}